package com.lvtulife.paas.gtd.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.CollectionUtil;
import com.lvtulife.common.component.exception.ErrMsg;
import com.lvtulife.common.service.impl.BaseServiceImpl;
import com.lvtulife.paas.gtd.entity.GtdStrategyTheme;
import com.lvtulife.paas.gtd.mapper.GtdStrategyThemeMapper;
import com.lvtulife.paas.gtd.service.GtdStrategyThemeServiceI;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * GtdStrategyTheme 表数据服务层接口实现类
 */
@Service
@Transactional(readOnly = true)
public class GtdStrategyThemeServiceImpl extends BaseServiceImpl<GtdStrategyThemeMapper, GtdStrategyTheme> implements GtdStrategyThemeServiceI {


    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Long saveTheme(String theme, Long userId) {
        Assert.notNull(userId, ErrMsg.NO_CONTENT.getMsg());
        checkTheme(theme);

        // 验证名称是否重复
        if (existTheme(theme)) {
            throw new RuntimeException(ErrMsg.EXIST_CONTENT.getMsg());
        }

        GtdStrategyTheme t = new GtdStrategyTheme();
        t.setTheme(theme);
        t.setHeat(0);
        t.setCreatedId(userId);
        t.setCreatedDt(new Date());

        checkOperation(baseMapper.insert(t));
        return t.getId();
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Boolean useTheme(Serializable id) {
        Assert.notNull(id, ErrMsg.NO_CONTENT.getMsg());

        GtdStrategyTheme theme = selectById(id);
        Assert.notNull(theme, ErrMsg.NO_CONTENT.getMsg());

        return retBool(baseMapper.addHeat(theme.getId()));
    }

    @Override
    public List<GtdStrategyTheme> findHotTheme(int size) {
        size = size <= 0 ? 1 : size;
        return baseMapper.findHeatTheme(size);
    }

    @Override
    public Boolean existTheme(String theme) {
        return retBool(selectCount(getDefaultWrapper(checkTheme(theme), false)));
    }


    @Override
    public GtdStrategyTheme findTheme(String theme) {
        List<GtdStrategyTheme> list = selectList(getDefaultWrapper(checkTheme(theme), false));
        return CollectionUtil.isNotEmpty(list) ? list.get(0) : null;
    }

    @Override
    public List<GtdStrategyTheme> findLikeTheme(String theme) {
        return selectList(getDefaultWrapper(checkTheme(theme), true));
    }

    @Override
    public Page<GtdStrategyTheme> findThemes(Page<GtdStrategyTheme> query) {
        // 热度排序
        query.setOrderByField("heat");
        query.setAsc(false);

        return selectPage(query, new EntityWrapper<>());
    }

    @Override
    public Page<GtdStrategyTheme> findLikeThemes(Page<GtdStrategyTheme> query, String theme) {
        // 热度排序
        query.setOrderByField("heat");
        query.setAsc(false);

        return selectPage(query, getDefaultWrapper(theme, true));
    }

    /* 检验theme的合法性 */
    private String checkTheme(String theme) {
        Assert.hasText(theme, ErrMsg.NO_CONTENT.getMsg());

        // 敏感词过滤
        //SensitivewordFilter filter = new SensitivewordFilter();
        //filter.getSensitiveWord(string, 1);

        return theme.trim();
    }

    /* 设置查询参数 */
    private EntityWrapper<GtdStrategyTheme> getDefaultWrapper(String theme, boolean isLike) {
        EntityWrapper<GtdStrategyTheme> wrapper = new EntityWrapper<>();
        if (isLike) {
            /* 模糊查询 */
            wrapper.like("theme", checkTheme(theme));
        } else {
            wrapper.where("theme={0}", checkTheme(theme));
        }
        return wrapper;
    }
}